package com.aaa.service.sas.impl;

import com.aaa.aspect.InsertAnn;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.sas.QuestionSas;
import com.aaa.entity.sas.QuestionSasVo;
import com.aaa.entity.sas.TestResultSas;
import com.aaa.entity.sas.ViewTestResultSas;
import com.aaa.dao.sas.ViewTestResultSasDao;
import com.aaa.service.sas.ViewTestResultSasService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ViewTestResultSas)表服务实现类
 *
 * @author makejava
 * @since 2022-09-02 19:25:36
 */
@Service("viewTestResultSasService")
public class ViewTestResultSasServiceImpl implements ViewTestResultSasService {
    @Resource
    private ViewTestResultSasDao viewTestResultSasDao;

    /**
     * 通过ID查询单条数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public ViewTestResultSas queryById() {
        return this.viewTestResultSasDao.queryById();
    }


    /**
     * 新增数据
     *
     * @param viewTestResultSas 实例对象
     * @return 实例对象
     */
    @Override
    public ViewTestResultSas insert(ViewTestResultSas viewTestResultSas) {
        this.viewTestResultSasDao.insert(viewTestResultSas);
        return viewTestResultSas;
    }


    /**
     * 通过主键删除数据
     *
     * @param
     * @return 是否成功
     */
    @Override
    public boolean deleteById() {
        return this.viewTestResultSasDao.deleteById() > 0;
    }

    /**
     * 查询结果
     *
     * @param pageSortEntity
     * @param viewTestResultSas
     * @return
     */
    @Override
    public List<ViewTestResultSas> queryResult(PageSortEntity pageSortEntity, ViewTestResultSas viewTestResultSas) {
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<ViewTestResultSas> viewTestResultSas1 = viewTestResultSasDao.queryPlanSas(viewTestResultSas);
        Page<ViewTestResultSas> page = (Page) viewTestResultSas1;
        return page;
    }

    @Override
    public List<ViewTestResultSas> showDetails(PageSortEntity pageSortEntity, ViewTestResultSas viewTestResultSas) {
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<ViewTestResultSas> viewTestResultSas1 = viewTestResultSasDao.showDetails(viewTestResultSas);
        Page<ViewTestResultSas> page = (Page) viewTestResultSas1;
        return page;
    }

    /**
     * 查询测试
     *
     * @return
     */
    @Override
    public List<ViewTestResultSas> queryAllPlan() {
        List<ViewTestResultSas> viewTestResultSas = viewTestResultSasDao.queryAllPlanSas();
        return viewTestResultSas;
    }

    /**
     * 删除、批量删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(int[] id) {
        List<Object> arrayList = new ArrayList<>();
        for (int i : id) {
            arrayList.add(i);
        }
        boolean delete = viewTestResultSasDao.delete(arrayList);
        return delete;
    }

    /**
     * 通过id查询结果
     *
     * @param viewTestResultSas
     * @return
     */
    @Override
    public ViewTestResultSas queryResultById(ViewTestResultSas viewTestResultSas) {
        return viewTestResultSasDao.queryResultById(viewTestResultSas);
    }

    /**
     * 报表统计 查询
     *
     * @return
     */
    @Override
    public Map<String, Integer> queryAll() {
        int normal = 0;
        int mild = 0;
        int moderate = 0;
        int severe = 0;
        List<ViewTestResultSas> viewSass = viewTestResultSasDao.queryAll();
        for (ViewTestResultSas viewSas : viewSass) {
            double forward = viewSas.getForward();
            double inversion = viewSas.getInversion();
            double sum = (forward + inversion) * 1.25;
            if (sum <= 50) {
                normal++;
            } else if (sum >= 50 && sum <= 59) {
                mild++;
            } else if (sum >= 60 && sum <= 69) {
                moderate++;
            } else {
                severe++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("normal", normal);
        map.put("mild", mild);
        map.put("moderate", moderate);
        map.put("severe", severe);
        return map;
    }

    @Override
    public String queryAllSasById(Integer id) {
        ViewTestResultSas viewTestResultSas = viewTestResultSasDao.queryAllSasById(id);
        double forward = viewTestResultSas.getForward();
        double inversion = viewTestResultSas.getInversion();
        double sum = forward + inversion;
        if (sum <= 50) {
            return "views/sas/SasNormal.jsp";
        } else if (sum < 60) {
            return "views/sas/SasMild.jsp";
        } else if (sum < 70) {
            return "views/sas/SasModerate.jsp";
        } else {
            return "views/sas/SasSevere.jsp";
        }
    }

    @Override
    public List<QuestionSasVo> queryDetail(Integer testerId) {
        List<QuestionSasVo> questionSasVos = viewTestResultSasDao.queryDetail(testerId);
        return questionSasVos;
    }
}
